/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { Component } from "react";
import PropTypes from "prop-types";
import KbModal from "@/components/KbModal";
import StandardTable from "@/components/StandardTable";
import { Button } from "antd";
import { connect } from "dva";
import DownloadBar from "@/components/DownloadBar";
import { debounce } from "lodash";
import pageConfig from "./orderPageConfig";

@connect(({ loading, orderRecord }) => ({
  orderRecord,
  getDownloadTaskList: loading.effects["orderRecord/getDownloadTaskList"],
  getDownloadTask: loading.effects["orderRecord/getDownloadTask"],
}))
export default class DownLoadModal extends Component {
  handleTableChange = debounce(
    pagination => {
      const { dispatch, type, sourceType } = this.props;
      const { statisticsGetDownLoadList, getDownLoadList } = this.actionTypes;
      const isWS = sourceType == "derivative";
      const isYZ = sourceType == "yz";
      const isStatistics = type === "statistics";

      const url = isWS || isYZ || isStatistics ? statisticsGetDownLoadList : getDownLoadList;

      const payload = { page: pagination.current };

      if (isWS) {
        payload.type = isStatistics ? 5 : 4;
      }

      if (isYZ) {
        payload.type = isStatistics ? 7 : 6;
      }

      dispatch({
        type: url,
        payload,
      });
    },
    500,
    {
      leading: true,
      trailing: false,
    }
  );

  static propTypes = {
    visible: PropTypes.bool.isRequired,
    onOk: PropTypes.func.isRequired,
    hideModal: PropTypes.func.isRequired,
    sourceType: PropTypes.string.isRequired,
  };

  constructor(props) {
    super(props);
    const { sourceType } = props;
    this.columns = [
      {
        title: "任务开始时间",
        dataIndex: "exec_time",
        key: "exec_time",
        align: "center",
      },
      {
        title: "任务状态",
        dataIndex: "status_text",
        key: "status_text",
        width: 90,
        align: "center",
      },
      {
        title: "名称",
        dataIndex: "file_name",
        key: "file_name",
        align: "center",
      },
      {
        align: "center",
        render: (text, record) => {
          let disabled = true;
          if (record.status == 1) {
            disabled = false;
          }
          return (
            <DownloadBar href={record.url}>
              <Button disabled={disabled} type="primary" icon="download">
                导出
              </Button>
            </DownloadBar>
          );
        },
      },
    ];
    this.pageConfig = pageConfig.call(this, sourceType); // sourceType 区分业务员（operator），微商（derivative），驿站（yz）
    const { actionTypes } = this.pageConfig;
    this.actionTypes = actionTypes;
  }

  componentDidUpdate(prevProps) {
    if (prevProps.visible !== this.props.visible && !this.props.visible) {
      const { dispatch, type } = this.props;
      const isRecord = type == "record"; // 判断是单号记录页面还是单号使用统计页面
      const payload = isRecord
        ? {
            // 清空单号记录查看下载任务的列表数据
            downLoadTask: {
              list: null,
              pagination: null,
            },
          }
        : {
            // 清空单号使用统计查看下载任务的列表数据
            downLoadList: {
              list: null,
              pagination: null,
            },
          };

      dispatch({
        type: "orderRecord/save",
        payload,
      });
    }
  }

  onOk = () => {
    const { hideModal } = this.props;
    hideModal();
  };

  render() {
    const {
      visible,
      hideModal,
      title,
      type,
      getDownloadTaskList,
      getDownloadTask,
      orderRecord: { downLoadTask, downLoadList },
      sourceType,
    } = this.props;

    const isStatistics = type === "statistics";
    const isWs = sourceType == "derivative";
    const isYZ = sourceType === "yz";

    const data = isWs || isYZ || isStatistics ? downLoadList : downLoadTask;

    const loading = isStatistics ? getDownloadTask : getDownloadTaskList;

    return (
      <KbModal visible={visible} onCancel={hideModal} title={title} okText={null} width="700">
        <StandardTable
          columns={this.columns}
          loading={loading}
          data={data}
          rowKey={record => record.exec_time}
          onChange={this.handleTableChange}
        />
      </KbModal>
    );
  }
}
